package svc

import (
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/rest"
	"product/authorization/model/permission"
	"product/authorization/model/tenants"
	"product/authorization/model/users"
	"product/authorization/serve/internal/config"
	"product/authorization/serve/internal/middleware"
)

type ServiceContext struct {
	Config           config.Config
	UsersModel       users.UsersModel
	TenantModel      tenants.TenantsModel
	PermissionsModel permission.PermissionsModel
	UserMiddleware   rest.Middleware
	LogMiddleware    rest.Middleware
	Redis            *redis.Redis
}

func NewServiceContext(c config.Config) *ServiceContext {
	mysqlConnection := sqlx.NewMysql(c.DB.DataSource)
	return &ServiceContext{
		Config:           c,
		UsersModel:       users.NewUsersModel(mysqlConnection),
		TenantModel:      tenants.NewTenantsModel(mysqlConnection),
		PermissionsModel: permission.NewPermissionsModel(mysqlConnection),
		UserMiddleware:   middleware.NewUserMiddleware().Handle,
		LogMiddleware:    middleware.NewLogMiddleware().Handle,
		Redis:            redis.MustNewRedis(c.RedisConfig),
	}
}
